package com.gitee.neuray.security.generator.utils;

import com.gitee.neuray.security.common.exception.BaseException;
import org.apache.commons.lang.WordUtils;
import org.springframework.util.StringUtils;

/**
 * @author: zhhongyu
 * @description: 字符串工具类
 * @since: Create in 15:06 2020/2/18
 */
public class StringUpperCaseUtils {
    /**
     * 将首字母转为大写
     * @param s
     * @return
     */
    public static String toUpperCaseFirstOne(String s) {
        checkBlank(s);
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * 首字符小写
     * @param s
     * @return
     */
    public static String toLowerCaseFirstOne(String s) {
        checkBlank(s);
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * 校验字符串不能为空
     * @param str
     * @return
     */
    public static boolean checkBlank(String str){
        if(StringUtils.isEmpty(str)){
            throw new BaseException("id不能为空...");
        }
        return true;
    }

    /**
     * 列名转换成Java属性名
     */
    public static String columnToJava(String columnName) {
        return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
    }

    /**
     * 表名转换成Java类名
     */
    public static String tableToJava(String tableName, String tablePrefix) {
        if (org.apache.commons.lang.StringUtils.isNotBlank(tablePrefix)) {
            tableName = tableName.replace(tablePrefix, "");
        }
        return columnToJava(tableName);
    }
}
